package sg.edu.nus.ouroboros.activities;

import sg.edu.nus.ouroboros.R;
import sg.edu.nus.ouroboros.constants.AppConstants;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class LoginActivity extends Activity  {
	// public String IVLE_Token;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);  
/*		WebView wv = (WebView) findViewById(R.id.WebViewLogin);                  
        //WebSettings webSettings = wv.getSettings();
        //webSettings.setBuiltInZoomControls(true);
        wv.getSettings().setJavaScriptEnabled(true);
         //Register a new JavaScript interface called HTMLOUT 
        wv.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");

        wv.setWebViewClient(new WebViewClient() 
        {               
        	@Override               
        	public void onPageFinished(WebView view, String url) 
        	{     
        		//Log.d("url: " + url, "onPageFinished");
        		// when login is complete, the url will be login_result.ashx?r=0
        		if (url.indexOf("/api/login/login_result.ashx") > 0)
        		{
        			// When login is successful, there will be a &r=0 in the url. It also means the return data is the token itself.
        			if (url.indexOf("&r=0") > 0)
        			{
        				//Log.d("success", "onPageFinished");        				
        				//Log.i("onPageFinished - before loading javascript", "");
        				view.loadUrl("javascript:window.HTMLOUT.processHTML(document.getElementsByTagName('body')[0].innerHTML);");  
        				//Http;
        			}
        		}

        	}
        });

        wv.loadUrl("https://ivle.nus.edu.sg/api/login/?apikey="+AppConstants.APIKey);*/
		//Intent intent = new Intent("sg.edu.nus.oroboros.TEST");
		Intent intent = new Intent("sg.edu.nus.ouroboros.MAIN_SCREEN");
		startActivity(intent);
		


	}

	final Context myApp = this;

	/* An instance of this class will be registered as a JavaScript interface */
	class MyJavaScriptInterface
	{
		public void processHTML(String html)
		{
			String IVLE_Token = html;
			Intent intent = new Intent("sg.edu.nus.ouroboros.MAIN_SCREEN");
			//intent.putExtra(name, value)
			intent.putExtra("IVLE_token", IVLE_Token);
			startActivity(intent);
		}
	}

}